using System;
using Csla;
using ENT.PM.Datos.MysqlHelper;
using MySql.Data.MySqlClient;
using System.Data;

namespace ENT.PM.Core
{
    [Serializable]
    public class Roles : BusinessBase<Roles>
    {
        #region Business Methods

        private static PropertyInfo<string> NombreRolProperty = RegisterProperty<string>(o => o.NombreRol, "Name");
        public string NombreRol
        {
            get { return GetProperty(NombreRolProperty); }
            set { SetProperty(NombreRolProperty, value); }
        }

        private static PropertyInfo<string> DescripcionRolComponenteProperty = RegisterProperty<string>(o => o.DescripcionRol, "Name");
        public string DescripcionRol
        {
            get { return GetProperty(DescripcionRolComponenteProperty); }
            set { SetProperty(DescripcionRolComponenteProperty, value); }
        }

        #endregion

        #region Validation Rules

        protected override void AddBusinessRules()
        {
            // TODO: add validation rules
            //ValidationRules.AddRule(RuleMethod, "");
        }

        #endregion

        #region Authorization Rules

        protected override void AddAuthorizationRules()
        {
            // TODO: add authorization rules
            //AuthorizationRules.AllowWrite("Name", "Role");
        }

        private static void AddObjectAuthorizationRules()
        {
            // TODO: add authorization rules
            //AuthorizationRules.AllowEdit(typeof(Roles), "Role");
        }

        #endregion

        #region Factory Methods

        public static Roles NuevoRol()
        {
            return DataPortal.Create<Roles>();
        }

        public static Roles GetEditableRoot(int id)
        {
            return DataPortal.Fetch<Roles>(
              new SingleCriteria<Roles, int>(id));
        }

        public static void DeleteEditableRoot(int id)
        {
            DataPortal.Delete(new SingleCriteria<Roles, int>(id));
        }

        private Roles()
        { /* Require use of factory methods */ }

        #endregion

        #region Data Access

        [RunLocal]
        protected override void DataPortal_Create()
        {
            // TODO: load default values
            // omit this override if you have no defaults to set
            base.DataPortal_Create();
        }

        private void DataPortal_Fetch(SingleCriteria<Roles, int> criteria)
        {
            // TODO: load values
        }

        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Insert()
        {
            try
            {
                HelperMySql objMysqlHelper = HelperMySql.ObtenerConexion("");
                MySqlParameter[] ojbParametros = new MySqlParameter[2];
                ojbParametros[0] = new MySqlParameter("DESCRIPCION_ROL", MySqlDbType.Text);
                ojbParametros[1] = new MySqlParameter("NOMBRE_ROL", MySqlDbType.Text);
                ojbParametros[1].Direction = ParameterDirection.Output;

                ojbParametros[0].Value = ReadProperty(DescripcionRolComponenteProperty);
                ojbParametros[1].Value = ReadProperty(NombreRolProperty);
                objMysqlHelper.EjecutarStoreProcedure("Insertar_Rol", ojbParametros);
            }
            catch
            {
                throw;
            }

        }

        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Update()
        {
            // TODO: update values
        }

        //[Transactional(TransactionalTypes.TransactionScope)]
        //protected override void DataPortal_DeleteSelf()
        //{
        //    DataPortal_Delete(new SingleCriteria<Roles, int>(this.Id));
        //}

        [Transactional(TransactionalTypes.TransactionScope)]
        private void DataPortal_Delete(SingleCriteria<Roles, int> criteria)
        {
            // TODO: delete values
        }

        #endregion
    }
}
